package com.zff.wechat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zff.web.po.UserPo;
import com.zff.web.service.UserService;
/*import com.zff.wechat.util.ImageAction;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.servlet.ServletOutputStream;*/
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;*/

@Controller
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public UserPo userLogin(HttpServletRequest request, @RequestBody UserPo userpo) {
		UserPo po = userService.userLogin(userpo);
		HttpSession session = request.getSession();
		session.setAttribute("userName", po.getUserName());
		return po;
	}

	
	@RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public @ResponseBody int userRegister(HttpServletRequest request, @RequestBody UserPo userpo) {
		// MessageInfo messageInfo = new MessageInfo();
		// UserValidate userValidate = new UserValidate();
		// messageInfo = userValidate.userRepeatValidate(userpo,messageInfo);
		int userRegister = userService.userRegister(userpo);
		return userRegister;
	}

	/*@ResponseBody
	@RequestMapping(value = "/image")
	public void image(HttpSession session, HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		ImageAction imageAction = new ImageAction();
		String number = null;
		BufferedImage image = null;
		try {
			image = imageAction.execute();
			number = imageAction.getNumber();
			session = request.getSession();
			session.setAttribute("number", number);

		} catch (ImageFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		ServletOutputStream out = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		encoder.encode(image);
		out.flush();
		out.close();

	}*/
	
	@ResponseBody
	@RequestMapping(value = "/getsession",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public String getSession(HttpSession session, HttpServletRequest request, HttpServletResponse response){
		String value = session.getAttribute("number").toString();
		return value;
	}
	
	@ResponseBody
	@RequestMapping(value = "/faceAge", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public void faceAge(HttpServletRequest request, @RequestBody Object faceImg) {
		System.out.println(faceImg);
	}
}
